package com.acidsweet.zerolrucache.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Convert OTHER DATA to Byte Array
 * Restore Byte Array to OTHER DATA
 */
public class Converter {
    private static final String TAG=Converter.class.getSimpleName();

    public static byte[] String2Bytes(String value)
    {
        return value.getBytes();
    }
    public static String Bytes2String(byte[] bytes)
    {
        return new String(bytes);
    }
    public static byte[] Int2Bytes(int value)
    {
        return String2Bytes(String.valueOf(value));
    }
    public static int Bytes2Int(byte[] bytes)
    {
        return Integer.valueOf(Bytes2String(bytes)).intValue();
    }
    public static byte[] Short2Bytes(Short value)
    {
        return String2Bytes(String.valueOf(value));
    }
    public static Short Bytes2Short(byte[] bytes)
    {
        return Short.valueOf(Bytes2String(bytes)).shortValue();
    }
    public static byte[] Long2Bytes(Long value)
    {
        return String2Bytes(String.valueOf(value));
    }
    public static Long Bytes2Long(byte[] bytes)
    {
        return Long.valueOf(Bytes2String(bytes)).longValue();
    }
    public static byte[] Boolean2Bytes(boolean value)
    {
        return String2Bytes(String.valueOf(value));
    }
    public static boolean Bytes2Boolean(byte[] bytes)
    {
        return Boolean.valueOf(Bytes2String(bytes)).booleanValue();
    }
    public static byte[] Float2Bytes(float value)
    {
        return String2Bytes(String.valueOf(value));
    }
    public static float Bytes2Float(byte[] bytes)
    {
        return Float.valueOf(Bytes2String(bytes)).floatValue();
    }
    public static byte[] Double2Bytes(double value)
    {
        return String2Bytes(String.valueOf(value));
    }
    public static Double Bytes2Double(byte[] bytes)
    {
        return Double.valueOf(Bytes2String(bytes)).doubleValue();
    }
    public static Bitmap Bytes2Bitmap(byte[] bytes) {
        if (bytes.length != 0) {
            return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        } else {
            return null;
        }
    }
    public static byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }
    public static byte[] Drawable2Bytes(Drawable value)
    {
        return Bitmap2Bytes(Drawable2Bitmap(value));
    }
    public static Drawable Bytes2Drawable(byte[] bytes)
    {
        return Bitmap2Drawable(Bytes2Bitmap(bytes));
    }
    public static Bitmap Drawable2Bitmap(Drawable drawable) {
        Bitmap bitmap = Bitmap.createBitmap(
                drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        //canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }
    public static Drawable Bitmap2Drawable(Bitmap value)
    {
        return new BitmapDrawable(value);
    }

    /**
     * trans a Serializable Objecty to Byte Array
     * @throws IOException
     *                      User Must Catch The Exception In His/Her Program
     */
    public static byte[] Serializable2Bytes(Object value) throws IOException
    {
        byte[] bytes=null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(value);
        bytes=bos.toByteArray();
        bos.close();
        oos.close();
        return bytes;
    }

    /**
     * trans a Byte Array to  Serializable Objecty
     * @throws IOException ClassNotFoundException
     *                                              User Must Catch The Exception In His/Her Program
     */
    public static Object Bytes2Serializable(byte[] bytes) throws IOException, ClassNotFoundException {
        Object obj=null;
        ByteArrayInputStream bis=new ByteArrayInputStream(bytes);
        ObjectInputStream ois=new ObjectInputStream(bis);
        obj=ois.readObject();
        bis.close();
        ois.close();
        return obj;
    }

}
